/*
 * Copyright (c) [2020-2021] [http://www.zxpnet.com]
 */

package com.zxp.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import com.zxp.common.api.R;
import com.zxp.common.core.validatecode.captcha.CaptchaHelper;
import com.zxp.springboot.pojo.HelloUser;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * @author zxp
 * @date 2021/7/1 15:48
 */
@Controller
@Slf4j
public class AuthController {

    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;

    @Autowired
    private org.redisson.api.RedissonClient redissonClient;

    @GetMapping("/login")
    public String loginUI(){

        RLock lock = redissonClient.getLock("jim");
        log.debug(lock.getName());

        return "auth/login.html";
    }

    @GetMapping("/auth/reg")
    public String registerUI(){
        return "auth/reg.html";
    }


    @GetMapping("/images/captcha")
    @SneakyThrows
    public void captcha(
            @RequestParam(required = false) String uuid,
            @RequestParam(defaultValue = "80") Integer width,
            @RequestParam(defaultValue = "24") Integer height,
            @RequestParam(defaultValue = "4") Integer len,
            @RequestParam(defaultValue = "1") Integer type,
            HttpServletRequest request, HttpServletResponse response) throws Exception{

        Captcha captcha = CaptchaHelper.getEasyCaptcha(width, height, len, type);
        captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);

        String verCode = captcha.text(); // 生成的验证码
        log.debug("图片验证码："+verCode);
        if(StrUtil.isNotBlank(uuid)){
            redisTemplate.opsForValue().set(uuid,verCode,2, TimeUnit.MINUTES); // 前后端分享的项目，前端传一个uuid过来
        }
        HelloUser helloUser = HelloUser.randomUser();

        redisTemplate.opsForValue().set("jim", helloUser);
        
        CaptchaUtil.out(captcha, request, response); // 默认会保存到session当中 SESSION_KEY = "captcha";
    }

    /**
     * 登陆
     * @param username
     * @param password
     * @param code
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public R doLogin(String username, String password, String code,Boolean rememberMe, HttpSession session){

        session.setAttribute("user",HelloUser.randomUser());

        return R.success("登陆成功");
    }

    /**
     * 注册
     * @param username
     * @param password
     * @param code
     * @return
     */
    @RequestMapping("/auth/reg2")
    @ResponseBody
    public R doReg(String username, String password, String code){

        Object o = redisTemplate.opsForValue().get("jim");
        if(o!=null){
            return R.data((HelloUser)o);
        }

        return R.success("注册成功");
    }

    @RequestMapping("/auth/user")
    @ResponseBody
    public R user(String username, String password, String code){

        HelloUser user = HelloUser.randomUser();

        return R.data(user);
    }


}
